from __future__ import division

inputFileName = "/data5/team/weijiang/the_new_trip_of_feature_generation/wholeTrainingFileTempleteV3_output_feature_phase0_1.txt"
inputFileHandler = open(inputFileName,"r")

outputFileName = "/data5/team/weijiang/the_new_trip_of_feature_generation/wholeTrainingFileTempleteV3_output_feature_phase0_1_fix1.txt"
outputFileHandler = open(outputFileName,"w")

for line in inputFileHandler.readlines():
    lineElements = line.strip().split(" ")
    # check1
    if len(lineElements) != 49:
        print "error",line
        exit(1)
    
    # fix1 for the url_domain (by Wei to fit Maria)
    if lineElements[14] == "com" or lineElements[14] == "edu" or lineElements[14] == "gov" or lineElements[14] == "org" or lineElements[14] == "net":
        pass
    else:
        lineElements[14] = "other"
    
    # fix2 for the sum/avg/min/max term_rel_freq_in_doc (by Wei to fit Maria)
    # print lineElements[16],lineElements[-9],lineElements[-8],lineElements[-7],lineElements[-6]
    if float(lineElements[16]) != 0:
        lineElements[-9] = str( float(lineElements[-9]) / float(lineElements[16]) )
        lineElements[-8] = str( float(lineElements[-8]) / float(lineElements[16]) )
        lineElements[-7] = str( float(lineElements[-7]) / float(lineElements[16]) )
        lineElements[-6] = str( float(lineElements[-6]) / float(lineElements[16]) )
    else:
        lineElements[-9] = "0"
        lineElements[-8] = "0"
        lineElements[-7] = "0"
        lineElements[-6] = "0"
        
    # fix3 terms_in_header currently is 0 always, but it really deserve the same value of how the field terms_in_title does.
    lineElements[26] = lineElements[27]
    
    # fix4 frac_terms_in_header currently is 0 always, but it really deserve the same value of how the field frac_terms_in_title does.
    lineElements[31] = lineElements[32]
    
    # fix5: why the avg_term_BM25 is always 0, and this problem has been fixed from the original point.
    lineElements[45] = str( float(lineElements[44]) / int(lineElements[4]))
    
    
    #output logic
    newOutputLine = ""
    for element in lineElements:
        newOutputLine += element + " "
    
    outputFileHandler.write(newOutputLine + "\n")


inputFileHandler.close()
outputFileHandler.close()
print "all done"
    
    
    
